package cn.wolfcode.realm;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jishushen
 * @create 2021-02-18 13:44
 */
public class CrmRealm extends AuthorizingRealm {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    //将spring容器中的配置的凭证匹配器(HashedCredentialsMatcher)注入给当前的 Realm 对象
    @Autowired
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //创建SimpleAuthorizationInfo
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取认证方法传来的 员工对象
        Employee employee = (Employee) principalCollection.getPrimaryPrincipal();

        //判断员工 是否为admin
        if (employee.isAdmin()){
            //是超级管理员
            info.addRole("admin");
            info.addStringPermission("*:*"); //通配符
        }else {
            //根据用户的id查询用户所拥有的角色
            List<Role> roleList = roleMapper.selectRoleByEmployeeId(employee.getId());

            //根据用户的id查询它所对应的权限
            List<String> expression = permissionMapper.selectPermissionByEmployeeId(employee.getId());

            //创建一个集合，封装sn
            List<String> snList = new ArrayList<>();

            //判断用户的角色 和权限是否有值
            if (roleList.iterator().hasNext()&&roleList.iterator().next()==null || expression.iterator().hasNext()&&expression.iterator().next()==null){
                //没有值
                return info;
            }else {
                //有值
                //把每一个角色的sn 添加到snList集合中
                for (Role role : roleList) {
                    snList.add(role.getSn());
                }
                //添加 角色及权限
                info.addRoles(snList);
                info.addStringPermissions(expression);
            }
        }
        return info;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户名
        String username = (String) token.getPrincipal();
        //查询员工表
        Employee employee = employeeMapper.selectByName(username);
        //判断查询出来的用户是否存在
        if (employee == null){
            return null;
        }else {
            //身份信息，凭证信息（正确），盐，当前realm的名字
            return new SimpleAuthenticationInfo(employee,employee.getPassword(), ByteSource.Util.bytes(username),this.getName());
        }
    }
}
